package com.duan.laugh.event.service.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.duan.laugh.common.core.util.Assert;
import com.duan.laugh.common.event.constants.EventConstant;
import com.duan.laugh.event.mapper.EventModelMapper;
import com.duan.laugh.event.pojo.entity.EventModel;
import com.duan.laugh.event.pojo.vo.EventHandlerRuleVO;
import com.duan.laugh.event.pojo.vo.EventModelVO;
import com.duan.laugh.event.service.IEventHandlerRuleService;
import com.duan.laugh.event.service.IEventModelService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * <p>
 * 事件模型 服务实现类
 * </p>
 *
 * @author liutf
 * @since 2019-11-12
 */
@Service
public class EventModelServiceImpl extends ServiceImpl<EventModelMapper, EventModel> implements IEventModelService {

    @Autowired
    private EventModelMapper eventModelMapper;
    @Autowired
    private IEventHandlerRuleService eventHandlerRuleService;

    /**
     * 0010106 新增事件模型
     *
     * @param eventModelVO
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean insert(EventModelVO eventModelVO) {
        super.save(eventModelVO);
        List<EventHandlerRuleVO> eventHandlerRuleVOS = eventModelVO.getEventHandlerRules();
        if (CollUtil.isNotEmpty(eventHandlerRuleVOS)) {
            // 新增事件处理规则（0010206）
            eventHandlerRuleService.insertByEventModelVO(eventModelVO);
        }
        return true;
    }

    /**
     * 0010107 根据id修改事件模型
     *
     * @param eventModelVO
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean updateById(EventModelVO eventModelVO) {
        super.updateById(eventModelVO);
        List<EventHandlerRuleVO> eventHandlerRuleVOS = eventModelVO.getEventHandlerRules();
        if (CollUtil.isNotEmpty(eventHandlerRuleVOS)) {
            // 修改事件处理规则（0010207）
            eventHandlerRuleService.updateByEventModelVO(eventHandlerRuleVOS);
        }
        return true;
    }

    /**
     * 0010110 根据ids批量启用事件模型
     *
     * @param ids
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean updateStatusEnableByIds(String ids) {
        List<String> split = StrUtil.split(ids, ',', -1);
        return super.update(
                new UpdateWrapper<EventModel>()
                        .set("status", EventConstant.ENABLE)
                        .in("id", split)
        );
    }

    /**
     * 0010111 根据ids批量禁用事件模型
     *
     * @param ids
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean updateStatusDisableByIds(String ids) {
        List<String> split = StrUtil.split(ids, ',', -1);
        return super.update(
                new UpdateWrapper<EventModel>()
                        .set("status", EventConstant.DISABLE)
                        .in("id", split)
        );
    }

    /**
     * 0010112 根据id查看事件模型
     *
     * @param id
     * @return
     */
    @Override
    public EventModelVO selectEventModelVOById(Long id) {
        // 事件模型数据
        EventModel model = super.getById(id);
        Assert.isNull(model,"无此事件");
        // 0010211 根据事件模型id查询详细事件处理规则
        List<EventHandlerRuleVO> eventHandlerRuleVOS = eventHandlerRuleService.selectEventHandlerRuleVOSByModelId(id);
        EventModelVO eventModelVO = new EventModelVO();
        BeanUtils.copyProperties(model, eventModelVO);
        eventModelVO.setEventHandlerRules(eventHandlerRuleVOS);
        return eventModelVO;
    }

    /**
     * 0010113 根据url和method查询事件
     *
     * @param url
     * @param method
     * @return
     */
    @Override
    public List<EventModel> selectEventByUrlAndMethod(String url, String method) {
        return super.list(Wrappers.<EventModel>query().lambda()
                .eq(EventModel::getUrl, url)
                .eq(EventModel::getMethod, method)
                .eq(EventModel::getDelFlag, EventConstant.ENABLE)
                .eq(EventModel::getStatus, EventConstant.ENABLE));
    }
}
